/*  SSDateDisplayPrintView.m
 *  MESLib
 * 
 * Copyright 2004-2009 Starline Software Ltd
 * 
 * This file is part of MESLib.
 * 
 * MESLib is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MESLib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with MESLib. If not, see <http://www.gnu.org/licenses/>.
 */
//  Created by Tobias Haughton on Fri Jan 16 2004.
//

#import "SSDateDisplayPrintView.h"
#import "SSChartView.h"

@implementation SSDateDisplayPrintView

- (void)drawRect:(NSRect)rect {
   float xStart = (NSMinX([chartView visibleRect]) - 100);
   float xStop = NSMaxX([chartView visibleRect]);

   [[NSColor whiteColor] set];
   [NSBezierPath fillRect:[self bounds]];


   if([self displayMode] == SSMatrixDisplayMode) {
      NSEnumerator *trackingCellEnumerator = [trackingCellArrayByXPos objectEnumerator];
      id trackingCell;

      //get the tracking cells to draw themselves
      while (trackingCell = [trackingCellEnumerator nextObject]) {
         if(NSMinX([trackingCell cellFrame]) > xStart) {
            if(NSMinX([trackingCell cellFrame]) > xStop) {
               //we have passed the drawing area so break
               break;
            } else {
               //it is within the rect we are drawing
               [trackingCell drawInView:self];
            }
         }
      }
   } else {
      //overlap mode
      [[NSColor blackColor] set];

      NSMutableDictionary *overlapDisplayData = [(SSChartView *)chartView overlapDisplayData];
      NSDictionary *textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[NSFont fontWithName:@"Helvetica" size:10],
         NSFontAttributeName, nil, nil];

      NSBezierPath *separator = [NSBezierPath bezierPath];
      [separator moveToPoint:NSMakePoint(xStart, 15)];
      [separator lineToPoint:NSMakePoint(xStop, 15)];
      [separator setLineWidth:1];

      [[NSGraphicsContext currentContext] setShouldAntialias:NO];
      [separator stroke];

      [[overlapDisplayData objectForKey:@"secondaryPath"] stroke];
      [[NSGraphicsContext currentContext] setShouldAntialias:YES];

      //draw the strings
      NSMutableArray *unitStringArray = [overlapDisplayData objectForKey:@"unitDateStrings"];
      NSMutableArray *secondaryUnitStringArray = [overlapDisplayData objectForKey:@"secondaryUnitDateStrings"];
      NSMutableArray *unitStringPositionArray = [overlapDisplayData objectForKey:@"unitDateStringPositions"];
      NSMutableArray *secondaryUnitStringPositionArray = [overlapDisplayData objectForKey:@"secondaryUnitDateStringPositions"];
      int i = 0;

      while(i < [unitStringArray count]) {
         //draw unit strings
         [[unitStringArray objectAtIndex:i] drawAtPoint:NSMakePoint([[unitStringPositionArray objectAtIndex:i] floatValue], 2) withAttributes:textAttributes];
         i++;
      }

      i = 0;
      while(i < [secondaryUnitStringArray count]) {
         //draw secondary unit strings
         [[secondaryUnitStringArray objectAtIndex:i] drawAtPoint:NSMakePoint([[secondaryUnitStringPositionArray objectAtIndex:i] floatValue], 17) withAttributes:textAttributes];
         i++;
      }
   }
}


@end
